package com.dingjian.mcp.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Arrays;

@RestController
@RequestMapping("/")
public class AiController {
    private final DeepSeekChatModel chatModel;
//    private final OllamaChatModel chatModel;

    private final ChatClient deepseekChatClient;
//    private final ChatClient ollamaChatClient;

    @Autowired
    private SyncMcpToolCallbackProvider toolCallbackProvider;

    @Autowired
    public AiController(DeepSeekChatModel chatModel, ChatClient deepseekChatClient) {
        this.chatModel = chatModel;
        this.deepseekChatClient = deepseekChatClient;
    }


    @GetMapping(value = "/ai/stream",produces = "text/html;charset=UTF-8")
    public Flux<String> stream(@RequestParam(value = "message", defaultValue = "你好，请问我的MCP服务有哪些") String message) {
        ToolCallback[] toolCallbacks = toolCallbackProvider.getToolCallbacks();
        for (ToolCallback toolCallback : toolCallbacks) {
            System.out.println(toolCallback.getToolDefinition().name());
        }
        Flux<String> output = deepseekChatClient.prompt()
                .toolCallbacks(toolCallbacks)
                .system("你是一个MCP客户端，用于执行MCP工具")
                .user(message)
                .stream()
                .content();
        return output;
    }

}
